<?php

namespace App\Http\Controllers;

use App\Models\Coupon;
use App\Models\UserCoupons;
use Exception;
use Illuminate\Http\Request;

/**
 * 优惠券相关
 */
class CouponController extends Controller
{
    /**
     * 优惠券详情
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function coupon_info(Request $request)
    {
        try {
            $status   = 0;
            $data     = [];
            $goods_id = $request->get('goods_id');
            if (empty($goods_id)) {
                throw new Exception('缺少参数');
            }
            $coupon = Coupon::where(['coupon_id' => $goods_id])->first();
            //处理数据
            $data['kill_id']     = count($coupon->integral_kill) > 0 ? $coupon->integral_kill->kill_id : 0;
            $data['coupon_id']   = $coupon->coupon_id;
            $data['coupon_name'] = $coupon->coupon_name;
            $data['introduce']   = $coupon->introduce;
            $data['photo']       = $coupon->photo;
            $data['details']     = $coupon->details;
            $data['integral']    = count($coupon->integral_kill) > 0 ? $coupon->integral_kill->integral : 0;

            $status = 1;
            $msg    = '查询成功';
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return response()->json(['status' => $status, 'msg' => $msg, 'data' => $data]);
    }

    /**
     * 优惠券列表
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function counpon_list(Request $request)
    {
        try {
            $status  = 0;
            $data    = [];
            $user_id = $request->get('user_id');
            if (empty($user_id)) {
                throw new Exception('缺少参数');
            }
            $is_usable = $request->get('is_usable') ?? 0; //默认为可用
            if ($is_usable == 0) {
                $coupons = UserCoupons::where(['user_id' => $user_id, 'is_used' => 0])->get();
            } else if($is_usable == 1) {
                $coupons = UserCoupons::where(['user_id' => $user_id, 'is_used' => 1])->get();
            } else {
                throw new Exception('参数错误！');
            }
            //处理数据
            foreach ($coupons as $key => $value) {
                $data[$key]['user_coupon_id'] = $value->user_coupon_id;
                $data[$key]['coupon_id']      = $value->coupon_id;
                $data[$key]['is_used']        = $value->is_used;
                $data[$key]['coupon_name']    = $value->coupon->coupon_name;
                $data[$key]['money']          = sprintf('%.2f', $value->coupon->money);
                $data[$key]['limit_money']    = sprintf('%.2f', $value->coupon->limit_money);
                $data[$key]['range_type']     = $value->coupon->range_type; //1全部商家 2指定商家3指定行业
            }

            $status = 1;
            $msg    = '查询成功';
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return response()->json(['status' => $status, 'msg' => $msg, 'data' => $data]);
    }
}
